<?php namespace App\Http\Controllers\user\uc;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use Illuminate\Http\Request;
use Input;
use Validator;
use App\Models\UserProfile;
use App\Models\UserRoles;
use App\Models\Area;
use App\Models\Users;
use Session;
use App;

class ProfileController extends Controller {

	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
	public function index()
	{
		$uid = $this->getUid();
		$model = UserProfile::where('uid', $uid);
		if($model->count()){
			$data = $model->get()[0]->toArray();
			$data["province"] = Area::where("area_id", $data["province"])->pluck("area_name");
			$data["city"] = Area::where("area_id", $data["city"])->pluck("area_name");
			$data["area"] = Area::where("area_id", $data["area"])->pluck("area_name");
		}else{
			return App::abort(404);
		}

		$userRoles = UserRoles::orderBy("id", "desc")->get()->toArray();
		return view('user.uc.profile', ['data' => $data, "roles" => $userRoles])->withIsProfile(true);
	}

	public function editProfile()
	{
		$uid = $this->getUid();
		$model = UserProfile::where("uid", $uid);
		if($model->count())
		{
			$data = $model->get()[0]->toArray();
			$province = Area::where("parent_id", 1)->get()->toArray();
			$city = Area::where("parent_id", $data["province"])->get()->toArray();
			$area = Area::where("parent_id", $data["city"])->get()->toArray();
		}
		// dd($data);
		return view("user.uc.edit_profile", ["province" => $province, "city" => $city, "area" => $area])->withIsProfile(true);
	}

	public function profileList()
	{
		$uid = $this->getUid();
		// if(env('APP_DEBUG')){
		// 	$uid = 30;
		// }
		$data = UserProfile::where("uid", $uid);
		if($data->count()){
			$data = $data->get()[0];
			return $this->json(['data'=>$data]);
		}else{
			return App::abort(404);
		}
	}


	public function updateProfile()
	{

		$rule = array();
		$msg = array();
		$validator = Validator::make(Input::all(), $rule, $msg);
		if($validator->passes()){
			$uid = $this->getUid();
			// if(env('APP_DEBUG')){
			// 	$uid = 30;
			// }
			$model = UserProfile::where('uid', $uid);
			if($model->count()){
				$data = Input::all();
				$model->update($data);
				$uSession = Users::with(['profile'])->where("id", $uid)->get()[0]->toArray();
				Session::put("user", $uSession);
				return $this->json([]);
			}else{
				$data = Input::all();
				UserProfile::save($data);
			}
		}else{
			return self::error(self::DATA_IS_WRONG);
		}
	}


	public function updateFace()
	{
		$rule = array();
		$msg = array();
		$validator = Validator::make(Input::all(), $rule, $msg);
		if($validator->passes()){
			$uid = $this->getUid();
			$avatar = Input::get("avatar");
			$path = base_path() . "/public/upload/" . $avatar;
			$dir = base_path() . "/public/images/" . $uid . "/" . env("OTHERS_DIR");
			is_dir($dir) || mkdir($dir, 0777, true);
			if(is_file($path)){
				$newPath = $dir . '/' . $avatar;
				rename($path, $newPath);
			}
			$model = UserProfile::where('uid',$uid);
			$model->update(['avatar' => $avatar]);
			$user = Session::get("user");
			$user["profile"]["avatar"] = $avatar;
			Session::set("user", $user);
			return $this->json([]);
		}else{

		}
	}

	// 后台用户列表

	/**
	 * Show the form for creating a new resource.
	 *
	 * @return Response
	 */
	public function create()
	{
		//
	}

	/**
	 * Store a newly created resource in storage.
	 *
	 * @return Response
	 */
	public function store()
	{
		//
	}

	/**
	 * Display the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		//
	}

	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		//
	}

	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		//
	}

	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		//
	}
}
